package com.tin.triper.UI;

import com.tin.triper.Constants;
import com.tin.triper.R;
import com.tin.triper.Data.LocationProvider;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class TripSetupDialogActivity extends Activity {

	Button ok;
	Button cancel;
	Spinner tripTime;
	Spinner tripLength;
	EditText tripName;
	
	SharedPreferences prefs;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.trip_setup_dialog);
        prefs=getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE);

	    
	    ok =(Button) findViewById(R.id.trip_setup_dialog_ok_button);
	    cancel=(Button) findViewById(R.id.trip_setup_dialog_cancel_button);
	    tripTime=(Spinner) findViewById(R.id.trip_setup_dialog_trip_time_spinner);
	    tripLength=(Spinner) findViewById(R.id.trip_setip_dialog_trip_length_spinner);
	    tripName=(EditText) findViewById(R.id.trip_setup_dialog_trip_name_field);
	    
	    ArrayAdapter<CharSequence> adapterTripLength = ArrayAdapter.createFromResource(
	            this, R.array.trip_setup_dialog_trip_length_spinner_items, android.R.layout.simple_spinner_item);
	    adapterTripLength.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    tripLength.setAdapter(adapterTripLength);
	    
	    ArrayAdapter<CharSequence> adapterTripTime = ArrayAdapter.createFromResource(
	            this, R.array.trip_setup_dialog_trip_time_spinner_items, android.R.layout.simple_spinner_item);
	    adapterTripTime.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    tripTime.setAdapter(adapterTripTime);
	    
	    ok.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(tripName.getText().toString().equals("")){
					Toast.makeText(TripSetupDialogActivity.this, getString(R.string.error_message_no_trip_name), Toast.LENGTH_SHORT).show();
					prefs.edit().putBoolean(Constants.IS_RECORDING, false).commit();
				}else{
					prefs.edit().putBoolean(Constants.IS_RECORDING, true).commit();
					prefs.edit().putString(Constants.RECRODED_TRIP_NAME, tripName.getText().toString()).commit();
					prefs.edit().putString(Constants.TRIP_NAME, tripName.getText().toString()).commit();
					ContentValues values=new ContentValues();
					values.put(LocationProvider.TRIP_LENGTH,0);
					values.put(LocationProvider.TRIP_NAME,tripName.getText().toString());
					values.put(LocationProvider.TRIP_TIME,0);
					getContentResolver().insert(LocationProvider.CONTENT_URI_TRIPS, values);
					
					Cursor cursor=getContentResolver().query(LocationProvider.CONTENT_URI_TRIPS, new String[]{LocationProvider.TRIP_ID}, LocationProvider.TRIP_NAME+" ='"+tripName.getText().toString()+"'", null, null);
					if(cursor.moveToFirst())
						prefs.edit().putInt(Constants.RECORDED_TRIP_ID, cursor.getInt(0)).commit();
					cursor.close();
					if(tripTime.getSelectedItemId()!=AdapterView.INVALID_ROW_ID&&tripLength.getSelectedItemId()!=AdapterView.INVALID_ROW_ID){
						if(tripTime.getSelectedItemId()==0){
							prefs.edit().putInt(Constants.GPS_TIME_UPDATE, 54).commit();
						}else if(tripTime.getSelectedItemId()==1){
							prefs.edit().putInt(Constants.GPS_TIME_UPDATE, 108).commit();
						}else if(tripTime.getSelectedItemId()==2){
							prefs.edit().putInt(Constants.GPS_TIME_UPDATE, 216).commit();
						}else{
							prefs.edit().putInt(Constants.GPS_TIME_UPDATE, 432).commit();
						}
						if(tripLength.getSelectedItemId()==0){
							prefs.edit().putInt(Constants.GPS_DISTANCE_UPDATE, 50).commit();
						}else if(tripLength.getSelectedItemId()==1){
							prefs.edit().putInt(Constants.GPS_DISTANCE_UPDATE, 250).commit();
						}else if(tripLength.getSelectedItemId()==2){
							prefs.edit().putInt(Constants.GPS_DISTANCE_UPDATE, 500).commit();
						}else {
							prefs.edit().putInt(Constants.GPS_DISTANCE_UPDATE, 1000).commit();
						}
					}
					
					
					Intent intent=new Intent(TripSetupDialogActivity.this, RecordActivity.class);
					intent.putExtra(Constants.ACTIVITY_MODE, -1);
					startActivity(intent);
					finish();
				}
				
			}
		});
	    cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				prefs.edit().putBoolean(Constants.IS_RECORDING, false).commit();
				finish();
			}
		});
	}
	@Override
	protected void onResume() {
		super.onResume();
		LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			createGpsDisabledAlert();
		} 
	}
	private void createGpsDisabledAlert() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(getString(R.string.menu_message_no_gps))
				.setCancelable(false)
				.setPositiveButton(getString(R.string.menu_enable_gps),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								showGpsOptions();
							}
						});
		builder.setNegativeButton(getString(R.string.common_no),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						finish();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}
	private void showGpsOptions() {
		Intent gpsOptionsIntent = new Intent(
				android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		startActivity(gpsOptionsIntent);
	}
}
